<?php

class Timer
{
	public static function getMicrotime() // float
	{
		list($usec, $sec) = explode(' ', microtime());
		return ((float)$usec + (float)$sec);
	}

	private $times;
	private $init;

	public function __construct($message = ' init')
	{
		$this->times = array();
		$this->init  = Timer::getMicrotime();
		$this->addTimer($message);
	}
	
	public function addTimer($message = '')
	{
		$this->times[] = array($message, $this->getMicrotime());
	}
	
	public function getLastTimer()
	{
		$n = count($this->times);
		return 'Timer "'.$this->times[$n-1][0].'" > '.round(($this->times[$n-1][1] - $this->init), 3);
	}
	
	public function getLastTime()
	{
		$n = count($this->times);
		return round(($this->times[$n-1][1] - $this->init), 3);
	}
	
	public function __toString()
	{
		$result = '';
		$c = 0;
		foreach($this->times as $t)
		{
			$c++;
			if($c == 1)
			{
				$tmp = $t[1];
			}
			else
			{
				$tmp = round(($t[1] - $this->init), 3);
			}
			$result .= 'Timer '.$c.' "'.$t[0].'" > '.$tmp."\n";
		}
		return $result;
	}
}
